﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using UCL.Powersource;
using UCL.Powersource.Helpers;

namespace PowersourceUIToolkit
{

    /// <summary>
    /// 
    /// </summary>
    public static class HierarchicallySynchronizedLists
    {
        static ILog Log = LogManager.GetLog(typeof(HierarchicallySynchronizedLists));

        public static readonly DependencyProperty SynchronizedSelectedItemsProperty = 
            DependencyProperty.RegisterAttached(
            "SynchronizedSelectedItems", typeof(IList), typeof(HierarchicallySynchronizedLists), new PropertyMetadata(null, OnSynchronizedSelectedItemsChanged));

        private static void OnSynchronizedSelectedItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            Log.DebugMethod("OnSynchronizedSelectedItemsChanged: d={0},eold={1},enew={2},eprop={3} ", d, e.OldValue, e.NewValue, e.Property.Name);
            if (e.OldValue != null)
            {
                var oldIsc=e.OldValue as ISynchronizedCollection;
                if (oldIsc != null)
                {
                    Log.DebugMethod("Detaching {0} from listening to SelectedItemsCollection",oldIsc);
                    
                    oldIsc.DetachFromSelectedItemsCollection();
                }
            }
            if (e.NewValue != null)
            {
                var newIsc = e.NewValue as ISynchronizedCollection;
                if (newIsc != null)
                {
                    if (d is ListBox)
                    {
                        var sel = (ListBox)d;
                        newIsc.AttachToSelectedItemsCollection(sel.SelectedItems);
                        Log.DebugMethod("Attaching {0} to listen to events on SelectedItemsCollection of listBox {1}", newIsc.ToString(),sel.Name);
                    }
                    else
                        if (d is ListView)
                        {
                            var sel = (ListView)d;
                            newIsc.AttachToSelectedItemsCollection(sel.SelectedItems);
                            Log.DebugMethod("Attaching {0} to listen to events on SelectedItemsCollection of listview {1}", newIsc.ToString(), sel.Name);
                        }
                }
            }
            
        }
        [AttachedPropertyBrowsableForType(typeof(Selector))]
        public static IList GetSynchronizedSelectedItems(DependencyObject dp)
        {
            Log.DebugMethod();
            return (IList)dp.GetValue(SynchronizedSelectedItemsProperty);
        }


        public static void SetSynchronizedSelectedItems(DependencyObject dp, IList value)
        {
            Log.DebugMethod();
            dp.SetValue(SynchronizedSelectedItemsProperty,value);
        }

        //public static readonly DependencyProperty SelectedMemberNameProperty = DependencyProperty.RegisterAttached(
        //    "SynchronizedSelectedItemsProperty", typeof(String), typeof(HierarchicallySynchronizedLists), new PropertyMetadata("IsSelected", OnSynchronizedSelectedItemsChanged));

       
    }
}
